﻿using System.Net.Mail;
using System;
using System.Net.Mime;
using System.Collections;
using System.Net;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using System.Configuration;
using DPUWebPhet10.Models;
using System.Globalization;
namespace DPUWebPhet10.Utility
{
    class Email
    {

        private const String Host = "smtp.gmail.com";
        private const String EmailUser = "petchyodmongkut@gmail.com";
        private const String EmailPassword = "petch2013";

        public static void SendEmail(TB_APPLICATION_SCHOOL school,String registerdPassword,String culture)
        {

            String templateName = (culture.ToUpper().Equals("TH")) ? "AccountDetail_th.html" : "AccountDetail_en.html";
            String emailSubject = (culture.ToUpper().Equals("TH")) ? "การลงทะเบียนเข้าร่วมการแข่งขัน" : "การลงทะเบียนเข้าร่วมการแข่งขัน";
            String displanName = (culture.ToUpper().Equals("TH")) ? "การแข่งขันภาษาจีนเพชรยอดมงกุฎ ครั้งที่ 10 (นานาชาติ)" : "การแข่งขันภาษาจีนเพชรยอดมงกุฎ ครั้งที่ 10  (นานาชาติ)";

            MailDefinition mailDefinition = new MailDefinition();
            mailDefinition.BodyFileName = "~/Utils/Email-Templates/" + templateName;
            mailDefinition.From = EmailUser;




            //Create a key-value collection of all the tokens you want to replace in your template...
            ListDictionary ldReplacements = new ListDictionary();
            ldReplacements.Add("<%School Name%>", school.SCHOOL_NAME);
            ldReplacements.Add("<%User%>", school.SCHOOL_EMAIL.Trim());
            ldReplacements.Add("<%Password%>", registerdPassword.Trim());

            string mailTo = string.Format("{0} <{1}>", school.SCHOOL_NAME, school.SCHOOL_EMAIL);
            MailMessage mailMessage = mailDefinition.CreateMailMessage(mailTo, ldReplacements, new System.Web.UI.Control());
            mailMessage.From = new MailAddress(EmailUser, displanName);
            mailMessage.IsBodyHtml = true;
            mailMessage.Subject = emailSubject;
            mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;

            // smtp settings
            System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
            {
                smtpClient.Host = "smtp.gmail.com";
                smtpClient.Port = 587;
                smtpClient.EnableSsl = true;
                smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                smtpClient.Credentials = new NetworkCredential(EmailUser, EmailPassword);
                smtpClient.Timeout = 20000;
            }
            //SmtpClient smtpClient = new SmtpClient(ConfigurationManager.AppSettings["SMTPServer"].ToString(), 25);
            smtpClient.Send(mailMessage);
        }

 
    }
}
